package cn.com.jinhao.provider.user.controller;


import cn.com.jinhao.hotline.commons.base.constant.SystemConstant;
import cn.com.jinhao.hotline.commons.controller.BaseController;
import cn.com.jinhao.hotline.commons.util.RestResponse;
import cn.com.jinhao.provider.user.entity.Dept;
import cn.com.jinhao.provider.user.service.DeptService;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * HotLine
 * <p>Description: 部门接口 </p>
 *
 * @author ShaneMew
 * @version 1.0
 * @date 2019/11/25
 * @since 2019-11-25
 */
@RestController
@RequestMapping("depts")
@Api(tags = {"部门信息服务"})
public class DeptController extends BaseController {
    @Resource
    private DeptService deptService;

    @ApiOperation(value = "查询部门", notes = "1. 根据编码查询部门信息 \n 2. 查询所有部门  \n 3. 根据上级部门查询下级部门")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "部门编码", dataType = "String"),
        @ApiImplicitParam(name = "id", value = "部门ID", dataType = "String"),
        @ApiImplicitParam(name = "isAll", value = "是否查询所有部门", dataType = "Boolean", defaultValue = "false"),
        @ApiImplicitParam(name = "parentCode", value = "上级部门编码", dataType = "String"),
        @ApiImplicitParam(name = "pageNo", value = "当前页数", dataType = "Integer", defaultValue = "1"),
        @ApiImplicitParam(name = "pageSize", value = "每页显示条数", dataType = "Integer", defaultValue = "20")
    })

    @GetMapping
    public RestResponse<Object> queryDept(@RequestParam(required = false) String code, @RequestParam(required = false) Boolean isAll, @RequestParam(required = false) String parentCode, @RequestParam(required = false) Integer pageNo, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) String id) throws Exception {
        if (StringUtils.isNoneBlank(id)) {
            Dept dept = deptService.queryByPrimaryKey(id);
            return this.checkExist(dept);
        } else if (StringUtils.isNoneBlank(code)) {
            Dept dept = deptService.queryByDeptCode(code);
            return this.checkExist(dept);
        } else if (StringUtils.isNoneBlank(parentCode)) {
            return new RestResponse<>(deptService.queryByParentDeptCode(parentCode));
        } else {
            if (Optional.ofNullable(isAll).isPresent()) {
                if (isAll) {
                   return new RestResponse<>(deptService.queryAll(pageNo, pageSize));
                } else {
                    return new RestResponse<>(deptService.queryAll(0, 0));
                }
            } else {
                return new RestResponse<>(SystemConstant.ResponseCode.PARAM_ERROR, null);
            }
        }
    }

    @ApiOperation(value = "查询部门", notes = "通过部门编码查询部门名称(内部使用)")
    @GetMapping(value = "/queryNameByCode")
    public String queryNameByCode(@ApiParam(name = "code", value = "部门编码", required = true) @RequestParam String code) throws Exception {
        return deptService.getNameByCode(code);
    }

    @ApiOperation(value = "创建部门", notes = "创建部门")
    @PostMapping
    public RestResponse<Dept> addDept(@ApiParam(name = "dept", value = "部门信息", required = true) @RequestBody Dept dept) throws Exception {
        return this.executeMethod(dept, SystemConstant.DataOperType.DATA_OPER_TYPE_ADD.getValue());
    }

    @ApiOperation(value = "更新部门", notes = "更新部门信息")
    @PutMapping
    public RestResponse<Dept> updateDept(@ApiParam(name = "dept", value = "部门信息", required = true) @RequestBody Dept dept) throws Exception {
        if (StringUtils.isBlank(dept.getCode())) {
            throw new IllegalArgumentException("部门编码不能为空");
        }
        return this.executeMethod(dept, SystemConstant.DataOperType.DATA_OPER_TYPE_UPDATE.getValue());
    }

    @ApiOperation(value = "删除部门", notes = "根据编码删除部门")
    @DeleteMapping
    public RestResponse<Dept> deleteByCode(@ApiParam(name = "code", value = "部门编码", required = true) @RequestParam String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            throw new IllegalAccessException("部门编码不能为空");
        }
        return this.executeMethod(code, SystemConstant.DataOperType.DATA_OPER_TYPE_DEL.getValue());
    }

    private RestResponse<Dept> executeMethod(Object obj, int methodType) throws Exception {
        switch (Objects.requireNonNull(SystemConstant.DataOperType.getByValue(methodType))) {
            case DATA_OPER_TYPE_UPDATE:
                deptService.update((Dept) obj);
                break;
            case DATA_OPER_TYPE_DEL:
                deptService.delete(obj.toString());
                break;
            default:
                deptService.add((Dept) obj);
                break;
        }
        return new RestResponse<>(null);
    }
}
